package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.ProdTplBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.GetProductAndCatalogInfoCall;
import com.ymatou.iapi.productmanager.service.SaveProductTemplateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.business.service.SellerProductCallServiceV2;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import com.ymatou.iapi.sellerproduct.service.SaveProductTemplateCall;

public class Ts_GetProductAndCatalogInfo {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static GetProductAndCatalogInfoCall getProductAndCatalogInfoCall = new GetProductAndCatalogInfoCall();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("Get product detail");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getProductAndCatalogInfoCall.getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductAndCatalogInfo_001() {
		Logger.start(true, "Get product detail");
		try{
			SaveProductTemplateCall saveProductTemplateCall = new SaveProductTemplateCall();
			saveProductTemplateCall.deleteTemplate(sellerId);
			//	模版类型（1，尺码模版 2，买家须知 3，买手服务）
			ProdTplBean sizeTemplate = saveProductTemplateCall.saveTemplate(sellerId, 1);
			ProdTplBean buyerNoticeTemplate = saveProductTemplateCall.saveTemplate(sellerId, 2);
			ProdTplBean sellerServiceTemplate = saveProductTemplateCall.saveTemplate(sellerId, 3);
			
			//get property 
			Map<String, Object> thirdCategoryAndPropertyVal = SellerProductCallServiceV2.getThirdCategoryAndPropertyValue();
			
			//发布商品
			//JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl.json");
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.productmanager/AddSellerProductCall/addsellerproductcalltpl.json");
			Map<String, Object> updateMap = MapUtil.hashMap();
			updateMap.put("sellerId", sellerId);
			updateMap.put("sellerName", sellerName);
			updateMap.put("product->thirdCategoryId", thirdCategoryAndPropertyVal.get("ThirdCategory"));
			updateMap.put("product->propertyList", (JSONArray)thirdCategoryAndPropertyVal.get("PropertyList"));
			updateMap.put("product->descModel->sizeTemplateId", sizeTemplate.getTemplateId());
			updateMap.put("product->descModel->buyerNoticeId",buyerNoticeTemplate.getTemplateId());
			updateMap.put("product->descModel->sellerServiceId", sellerServiceTemplate.getTemplateId());	
			JsonBeanHelper.updateJsonBean(addProductBean, updateMap);
			
			String productId = ProductManagerCallServiceV2.addNewProduct(addProductBean).getJSONObject("Data").getString("ProductId");


			//属性检查
			SellerProductCallServiceV2.checkProdProperty(productId,(JSONArray)thirdCategoryAndPropertyVal.get("PropertyList"));
			
			//模板检查
			//check product template
			List<ProdTplBean> prodTplBeanList = Arrays.asList(sizeTemplate,buyerNoticeTemplate,sellerServiceTemplate);
			SellerProductCallServiceV2.checkTemplateWithProd(productId,prodTplBeanList);
		
			JSONObject ret = getProductAndCatalogInfoCall.callAndGetReturnData(sellerId, productId);
			
			JSONObject productInfo = ret.getJSONObject("Data").getJSONObject("ProductAndCatalogInfoOnSellerProduct");
			
			Logger.verifyEquals(productInfo.getJSONObject("SizeTemplate").getInt("SizeTemplateId"), sizeTemplate.getTemplateId(), "商品信息 返回校验 sizeTemplate");
			Logger.verifyEquals(productInfo.getJSONObject("SizeTemplate").getString("Name"), sizeTemplate.getTemplateName(), "商品信息 返回校验 sizeTemplate");
			Logger.verifyEquals(productInfo.getJSONObject("BuyerNotice").getInt("BuyerNoticeId"), buyerNoticeTemplate.getTemplateId(), "商品信息 返回校验 BuyerNotice");
			Logger.verifyEquals(productInfo.getJSONObject("BuyerNotice").getString("Name"), buyerNoticeTemplate.getTemplateName(), "商品信息 返回校验 BuyerNotice");
			Logger.verifyEquals(productInfo.getJSONObject("SellerService").getInt("SellerServiceId"), sellerServiceTemplate.getTemplateId(), "商品信息 返回校验 SellerService");
			Logger.verifyEquals(productInfo.getJSONObject("SellerService").getString("Name"), sellerServiceTemplate.getTemplateName(), "商品信息 返回校验 SellerService");

			SellerProductCallServiceV2.checkPropertyList( productInfo.getJSONArray("PropertyList"), Integer.parseInt(thirdCategoryAndPropertyVal.get("ThirdCategory").toString()), (JSONArray)thirdCategoryAndPropertyVal.get("PropertyList"));

		}catch(Exception e){
			Logger.fail(e);
		}
	}


	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductAndCatalogInfo_002() {
		Logger.start(true, "拼邮发货,国外发货商品");
		try{
			AddSellerProductRequest addProductBean = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			addProductBean.getProduct().setPackageDeliveryType(1);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProductBean);


			JSONObject ret = getProductAndCatalogInfoCall.callAndGetReturnData(sellerId, productId);
			Logger.verifyEquals("200",getProductAndCatalogInfoCall.getString("Code"),"验证code");

			JSONObject productInfo = ret.getJSONObject("Data").getJSONObject("ProductAndCatalogInfoOnSellerProduct");

			Logger.verifyEquals(addProductBean.getProduct().getPackageDeliveryType(),productInfo.getInt("PackageDeliveryType"),"验证PackageDeliveryType");
		}catch(Exception e){
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductAndCatalogInfo_003() {
		Logger.start(true, "拼邮发货,国内发货商品");
		try{
			AddSellerProductRequest addProductBean = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			addProductBean.getProduct().setPackageDeliveryType(2);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProductBean);


			JSONObject ret = getProductAndCatalogInfoCall.callAndGetReturnData(sellerId, productId);
			Logger.verifyEquals("200",getProductAndCatalogInfoCall.getString("Code"),"验证code");

			JSONObject productInfo = ret.getJSONObject("Data").getJSONObject("ProductAndCatalogInfoOnSellerProduct");

			Logger.verifyEquals(addProductBean.getProduct().getPackageDeliveryType(),productInfo.getInt("PackageDeliveryType"),"验证PackageDeliveryType");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductAndCatalogInfo_004() {
		Logger.start(true, "获取商品详情，以及推荐商品理由");
		try{
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			addProdReq.getProduct().setRecomReason("推荐推荐");
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();

			JSONObject ret = getProductAndCatalogInfoCall.callAndGetReturnData(sellerId, productId);
			Logger.verifyEquals("200",getProductAndCatalogInfoCall.getString("Code"),"验证code");

			JSONObject productInfo = ret.getJSONObject("Data").getJSONObject("ProductAndCatalogInfoOnSellerProduct");
 
			Logger.verifyEquals(addProdReq.getProduct().getRecomReason(),productInfo.getString("RecomReason"),"验证推荐理由");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductAndCatalogInfo_005() {
		Logger.start(true, "查询商品商品库id");
		try{
			Integer sellerId=20336378;
			AddSellerProductRequest addProdReq =ProductManagerCallServiceV2.initAddSellerSpuProduct(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			Thread.sleep(10000);
			JSONObject ret = getProductAndCatalogInfoCall.callAndGetReturnData(sellerId, productId);
			Logger.verifyEquals("200",getProductAndCatalogInfoCall.getString("Code"),"验证code");
			JSONObject productInfo = ret.getJSONObject("Data").getJSONObject("ProductAndCatalogInfoOnSellerProduct");
			Logger.verifyEquals(229,productInfo.getInt("SpuId"),"验证商品库id");
			Logger.verifyEquals(229,productInfo.getInt("SpuProductId"),"验证商品库id");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductAndCatalogInfo_006() {
		Logger.start(true, "查询商品，增加Tags");
		try{
			AddSellerProductRequest addSellerProductRequest = ProductManagerCallServiceV2.initAddSellerProductRequestV2(sellerId);
			addSellerProductRequest.getProduct().setProductTags(Arrays.asList("测试Tag1","测试Tag2"));
			String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductRequest).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Thread.sleep(3000);
			JSONObject ret = getProductAndCatalogInfoCall.callAndGetReturnData(288484, productId);
			Logger.verifyEquals("200",getProductAndCatalogInfoCall.getString("Code"),"验证code");
			JSONObject productInfo = ret.getJSONObject("Data").getJSONObject("ProductAndCatalogInfoOnSellerProduct");
			Logger.verifyEquals("测试Tag1",productInfo.getJSONArray("ProductTags").getString(0),"验证测试Tag1");
			Logger.verifyEquals("测试Tag2",productInfo.getJSONArray("ProductTags").getString(1),"验证测试Tag1");
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductAndCatalogInfo_007() {
		Logger.start(true, "newDeliveryAppRelease 为true时，查询老物流商品时候，是新物流类型");
		try{
			AddSellerProductRequest addProdReq =ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(3383);
			addProdReq.getProduct().setCatalogStatus(10);
			addProdReq.getProduct().setDeliveryCompanyType(1);
			addProdReq.getProduct().setDeliveryAging(5);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Thread.sleep(3000);
			JSONObject ret = getProductAndCatalogInfoCall.callAndGetReturnData(3383, productId);
			Logger.verifyEquals("200",getProductAndCatalogInfoCall.getString("Code"),"验证code");
			JSONObject productInfo = ret.getJSONObject("Data").getJSONObject("ProductAndCatalogInfoOnSellerProduct");
			Logger.verifyEquals(10, productInfo.getInt("CatalogStatus"), "验证catalogstatus变成新类型");
			Logger.verifyEquals(1, productInfo.getInt("DeliveryCompanyType"), "验证物流公司");
			Logger.verifyEquals(5, productInfo.getInt("DeliveryAging"), "验证物流时效");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
}
